<?php

namespace App\Http\Requests\Customers;

use App\Http\Requests\ApiRequest;
use App\Models\Customers\Customer;
use App\Services\EasySms\EasySmsService;
use App\Services\Utils\Guard;
use Illuminate\Http\UploadedFile;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;

/**
 * @property UploadedFile image
 * @property string fullname
 * @property string email
 * @property string telephone
 * @property string dial_code
 */
class UpdateCustomerRequest extends ApiRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $customer_id = Guard::customer()->customer_id;

        return [
            'image'     => 'nullable|image|max:2048',
            'fullname'  => 'required|string',
            'email'     => ['required', 'email', Rule::unique('customer')->ignore($customer_id, 'customer_id')],
            'telephone' => ['required', 'string'],
            'dial_code' => 'nullable|int'
        ];
    }

    public function afterValidator(Validator $validator)
    {
        if (! $this->dial_code) {
            $this->dial_code = EasySmsService::COUNTRY_CODE_CN;
        }
    }
}
